using System;
using FluentNHibernate.AutoMap;
using FluentNHibernate.Conventions;
using FluentNHibernate.Mapping;
using Tools.Common.Reflection;
using Webshop20.Domain.Customers.Core;

namespace Webshop20.Infrastructure.Persistence.NH.AutoMappings.Conventions.Particular
{
    //public class CustomerConventions : IClassConvention
    //{

    //    public bool Accept(IClassMap target)
    //    {
    //        return target.EntityType.Name == "Customer";
    //    }

    //    public void Apply(IClassMap target)
    //    {
    //        var autoMap = (AutoMap<Customer>)target;
    //        autoMap.Map(x => x.EMail).Unique().Not.Nullable();
    //    }
    //}

    public class CustomerConventions : IPropertyConvention, IReferenceConvention
    {

        public bool Accept(IProperty target)
        {
            return target.EntityType.Name == "Customer";
        }

        public void Apply(IProperty target)
        {
            if (target.Property.Name == "EMail")
                target
                    .Unique()
                    .Not.Nullable();
        }

        public bool Accept(IManyToOnePart target)
        {
            
            return target.EntityType.Name == typeof (Customer).Name;
        }

        public void Apply(IManyToOnePart target)
        {

            if (target.Property.Name == ReflectAbout<Customer>.GetProperty(x => x.Payment).Name)
            {
                target.Cascade.All();
                var customerManyToOnePart = (ManyToOnePart<object>)target;
                customerManyToOnePart.Not.LazyLoad();
            }



        }
    }
}